package com.ls.blog.handler;

import com.ls.blog.exception.*;
import com.ls.blog.result.JsonResult;
import com.ls.blog.result.ResultCode;
import com.ls.blog.utils.ResultTool;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @program: myblog
 * @author: lishun
 * @create: 2021-07-05 15:30
 **/
@ControllerAdvice
@ResponseBody
public class GlobalExceptionHandler {

    /**
     * token过期
     * @param ex
     * @return
     */
    @ExceptionHandler(TokenExpiredException.class)
    public JsonResult tokenExpired(Exception ex){
        ex.printStackTrace();
        return ResultTool.fail(ResultCode.TOKEN_EXPIRED);
    }

    /**
     * token没有发现
     * @param ex
     * @return
     */
    @ExceptionHandler(TokenNotFoundException.class)
    public JsonResult tokenNotFound(Exception ex){
        ex.printStackTrace();
        return ResultTool.fail(ResultCode.TOKEN_NOT_FOUNT);
    }

    /**
     * token解析失败
     * @param ex
     * @return
     */
    @ExceptionHandler(TokenParseException.class)
    public JsonResult tokenParse(Exception ex){
        ex.printStackTrace();
        return ResultTool.fail(ResultCode.TOKEN_PARSE_ERROR);
    }


    /**
     * 没有发现用户异常
     * @param ex
     * @return
     */
    @ExceptionHandler(NofindUserException.class)
    public JsonResult nofindUser(Exception ex){
        ex.printStackTrace();
        return ResultTool.fail(ResultCode.USER_ACCOUNT_NOT_EXIST) ;
    }


    /**
     * 用户存在异常
     * @param ex
     * @return
     */
    @ExceptionHandler(UserExitException.class)
    public JsonResult userExit(Exception ex){
        ex.printStackTrace();

        return ResultTool.fail(ResultCode.USER_ACCOUNT_ALREADY_EXIST);
    }
    /**
     * 参数异常的处理逻辑
     * @param ex
     * @return
     */
    @ExceptionHandler(ParameterIsNullException.class)
    public JsonResult parameterIsNull(Exception ex){
        ex.printStackTrace();
        return ResultTool.fail(ResultCode.PARAM_IS_BLANK);
    }


    /**
     * 最大的异常，写在最后
     * @param ex
     * @return
     */
    @ExceptionHandler(Exception.class)
    public JsonResult doException(Exception ex){
        ex.printStackTrace();
        return ResultTool.fail();
    }




}
